package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.template.biz.service.sys.SysDeptService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.request.sys.SysDeptAddRequest;
import com.liliudong.template.common.request.sys.SysDeptModifyRequest;
import com.liliudong.template.common.request.sys.SysDeptPageRequest;
import com.liliudong.template.common.entity.sys.SysDept;
import com.liliudong.template.common.response.sys.SysDeptDetailResponse;
import com.liliudong.template.common.response.sys.SysDeptListResponse;
import com.liliudong.template.common.response.sys.SysDeptPageResponse;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @author liudo
 * @date 2024-08-01 19:44:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dept")
public class SysDeptController {

	private final SysDeptService sysDeptService;

	/**
	 * 添加
	 *
	 * @param sysDeptAddRequest 新增dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysDeptAddRequest sysDeptAddRequest) {
		sysDeptService.addOne(sysDeptAddRequest);
		return R.ok();
	}

	/**
	 * 删除
	 *
	 * @param id 主键
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:delete")
	@DeleteMapping("/{id}")
	public R<?> deleteById(@PathVariable Long id) {
		sysDeptService.deleteById(id);
		return R.ok();
	}

	/**
	 * 修改
	 *
	 * @param id 主键
	 * @param sysDeptModifyRequest 更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:dept:modify")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id,
	                       @RequestBody @Validated SysDeptModifyRequest sysDeptModifyRequest) {
		sysDeptService.modifyById(id, sysDeptModifyRequest);
		return R.ok();
	}

	/**
	 * 详情
	 *
	 * @param id 主键
	 * @return {@link R }<{@link SysDeptDetailResponse }>
	 */
	@SaCheckPermission("sys:dept:detail")
	@GetMapping("/detail/{id}")
	public R<SysDeptDetailResponse> detailById(@PathVariable Long id) {
		SysDeptDetailResponse sysDeptDetailVO = sysDeptService.detailById(id);
		return R.ok(sysDeptDetailVO);
	}

	/**
	 * 分页查询
	 * @param page            				 页面
	 * @param size            				 大小
	 * @param sysDeptPageRequest 分页查询条件
	 * @return {@link R }<{@link Page< SysDeptPageResponse > }>
	 */
	@SaCheckPermission("sys:dept:list")
	@GetMapping("/page/{page}/{size}")
	public R<Page<SysDeptPageResponse>> queryPage(@PathVariable Long page, @PathVariable Long size, SysDeptPageRequest sysDeptPageRequest) {
		Page<SysDeptPageResponse> pageData = sysDeptService.queryPage(page, size, sysDeptPageRequest);
		return R.ok(pageData);
	}

	/**
	 * 查询树
	 *
	 * @return {@link R }<{@link List }<{@link Tree }<{@link Long }>>>
	 */
	@SaCheckPermission("sys:dept:list")
	@GetMapping("/tree")
	public R<List<Tree<Long>>> queryTree() {
		List<Tree<Long>> tree =  sysDeptService.queryTree();
		return R.ok(tree);
	}

	/**
	 * 列出所有
	 *
	 * @return {@link R }<{@link List }<{@link SysDept }>>
	 */
	@SaCheckPermission("sys:dept:list")
	@GetMapping("/list")
	public R<List<SysDeptListResponse>> queryList() {
		List<SysDeptListResponse> sysDeptListVOS = sysDeptService.listAs(QueryWrapper.create(), SysDeptListResponse.class);
		return R.ok(sysDeptListVOS);
	}
}
